package com.lxf.cn.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lxf.cn.uitl.Code;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * Created by liaoxuefeng on 2018/11/28.
 */
public class ReturnHandler implements HandlerMethodReturnValueHandler {


    private final HandlerMethodReturnValueHandler delegate;

    public ReturnHandler(HandlerMethodReturnValueHandler delegate) {
        this.delegate = delegate;
    }

    @Override
    public boolean supportsReturnType(MethodParameter returnType) {
        return delegate.supportsReturnType(returnType);
    }

    @Override
    public void handleReturnValue(Object returnValue, MethodParameter returnType,
                                  ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

        JSONObject result = new JSONObject();

        result.put("exceptionCode", Code.SUCCESS.code);
        result.put("exceptionMsg", "success");
        result.put("bizContent", JSON.parse(JSON.toJSONString(returnValue)));

        delegate.handleReturnValue(result, returnType, mavContainer, webRequest);
    }


}
